﻿#ifndef WIDGET_H
#define WIDGET_H

#include <QMap>
#include <QRect>
#include <QWidget>
#include <QThread>
#include <QCamera>
#include <QCameraImageCapture>

#include "Partner.h"
#include "LoadAudio.h"
#include "SendImgMsg.h"
#include "MyTcpSocket.h"
#include "RecvMsgEmit.h"
#include "SendAudioMsg.h"
#include "SendCommonMsg.h"
#include "MyVideoSurface.h"

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
   ~Widget();

signals:
    void startAudioCollect();
    void stopAudioCollect();
    void volumnChange(int);

    void pushImg(QImage);

private:
    static QRect            s_pos;

    Ui::Widget              *ui;

    bool                    m_isCreateMeeting;
    bool                    m_isJoinMeeting;

    quint32                 m_mainWindowIp;             // 主屏幕上的显示视频者的IP
    QStringList             m_lstIp;                    // 所有的ip列表

    QMap<quint32, Partner *>    m_roomPartners;

    MyTcpSocket             *m_tcpSocket;
    RecvMsgEmit             *m_recvMsgTransmitter;

    SendComMsg              m_sendComMsg;

    SendAudioMsg            *m_sendAudioMsg;
    QThread                 *m_sendAudioThread;
    LoadAudio               *m_loadAudio;

    bool                    m_isOpenCamera;
    QCamera                 *m_camera;
    QCameraImageCapture     *m_imageCapture;
    MyVideoSurface          *m_videoSurface;

    SendImgMsg              *m_sendImgMsg;
    QThread                 *m_sendImgThread;

    void paintEvent(QPaintEvent *event) override;

    void closeImg(quint32 ip);

    Partner *addPartner(quint32);
    void removePartner(quint32);
    void clearPartner();

private slots:
    void on_m_btnSendText_clicked();

    void on_m_btnConnectSever_clicked();

    void onSolveRecvMsg(MESG *);
    void onRecvIp(quint32);

    void on_m_btnCreatMeeting_clicked();
    void on_m_btnQuitMeeting_clicked();
    void on_m_btnJoinMeet_clicked();

    void onCameraImageCapture(QVideoFrame);
    void onCameraError(QCamera::Error);

    void on_m_btnOpenAudio_clicked(bool checked);
    void on_m_btnOpenVideo_clicked();

    void onAudioError(QString);
    void onSpeaks(QString);

    void on_m_hsdrSetVolume_valueChanged(int value);
};
#endif // WIDGET_H
